﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models;
using System.Data.SqlClient;
namespace jiaju.Areas.Back.Models.DAL
{
    public class PicutreService
    {
        //1.获取商品图片并且分页
        public static List<Picutre> getPicutreList(int page, int limit)
        {
            string sql = string.Format("select top({0}) * from dbo.picture where Id not in (select top(({1}-1) * {0}) Id from dbo.picture)", limit, page);          
            List<Picutre> list = new List<Picutre>();
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];           
                string ImgUrl = (string)sdr["ImgUrl"];
                int ProductId = (int)sdr["ProductId"];
                Picutre art = new Picutre()
                {
                    Id = Id,                 
                    ImgUrl = ImgUrl,
                    ProductId = ProductId,
                };
                list.Add(art);
            }
            return list;
        }
        //2.新增商品图片
        public static int AddPicutre(Picutre art)
        {
            string sql = string.Format(@"insert into dbo.picture (ImgUrl,ProductId) values('{0}',{1})", art.ImgUrl,art.ProductId);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        //通过Id获取数据详情
        public static Picutre GetById(string ArtId)
        {
            string sql = "select * from dbo.picture where Id=" + ArtId;
            Picutre picutre = null;
            SqlDataReader sdr = DBHelp.ExecuteReader(sql);
            while (sdr.Read())
            {
                int Id = (int)sdr["Id"];            
                string ImgUrl = (string)sdr["ImgUrl"];              
                int ProductId = (int)sdr["ProductId"];
                picutre = new Picutre()
                {
                    Id = Id,
                    ImgUrl = ImgUrl,
                    ProductId = ProductId,
                };
            }
            return picutre;
        }
        //编辑
        public static int EditPicutre(Picutre art)
        {
            String sql = string.Format(@"UPDATE dbo.picture SET ImgUrl='{0}'  WHERE Id={1}", art.ImgUrl, art.Id);
            int num = DBHelp.ExecuteNonQuery(sql);
            return num;
        }
        //删除
        public static int DelPicutre(String Id)
        {
            //1.商品图片
            String sqlPhoto = "delete from dbo.picture where Id="+Id;
            int num= DBHelp.ExecuteNonQuery(sqlPhoto);           
            return num;
        }      
    }
}